package com.yubest.security.exception;

import com.yubest.security.bean.Response;
import com.yubest.security.consts.Consts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *
 */
@RestControllerAdvice
@Slf4j
public class RestExceptionHandler {

    @ExceptionHandler(AccessDeniedException.class)
    public <T> Response<T> exceptionHandler(AccessDeniedException e) {
        log.error("AccessDeniedException异常", e);
        return Response.error(Consts.NOT_PERMIT_CODE, "无操作权限");
    }

    @ExceptionHandler(Exception.class)
    public <T> Response<T> exceptionHandler(Exception e) {
        log.error("Exception异常", e);
        String msg = e.getMessage();
        return Response.error(msg != null ? msg : "未知错误");
    }

}
